package com.biz.primus.ms.commodity.dao.po;

import com.biz.primus.base.enums.SaleChannel;
import com.biz.primus.base.enums.commodity.IStatus;
import com.biz.primus.base.enums.vendor.VendorType;
import com.biz.primus.base.vo.ExtendProductVo;
import com.biz.primus.base.vo.commodity.SaleStatus;
import com.biz.primus.commodity.enums.GbStatus;
import com.biz.primus.commodity.enums.GoodsKind;
import com.biz.primus.common.utils.JsonUtils;
import com.biz.primus.ms.base.jpa.converter.ListStringConverter;
import com.biz.primus.ms.base.jpa.po.BaseEntity;
import com.biz.primus.ms.commodity.dao.converter.SaleChannelEnumSetConverter;
import com.google.common.collect.Lists;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * 商品 Po
 *
 * @author david-liu
 * @date 2016年12月14日
 * @reviewer
 * @see
 */
@Data
@Entity
@Table(name = "pro_product")
public class Product extends BaseEntity implements Serializable {

    private static final long serialVersionUID = -4560453072255817874L;

    /**
     * 商家 ID
     */
    @Column
    private Long vendorId;

    /**
     * 商品类型(A类/B类)
     */
    @Column
    @Enumerated(EnumType.STRING)
    private VendorType productType;

    /**
     * 商品名称
     */
    @Column(length = 64)
    private String name;

    /**
     * 商品称
     */
    @Column(length = 64)
    private String shortName;

    /**
     * 英文简称
     */
    @Column(length = 64)
    private String englishShortName;

    /**
     * 英文全称
     */
    @Column(length = 64)
    private String englishFullName;

    /**
     * 商品主条码
     */
    @Column(length = 18)
    private String barCode;


    /**
     * 商品子标题
     */
    @Column(length = 50)
    private String subTitle;

    /**
     * 副标题头
     */
    @Column(length = 255)
    private String subHeadingMark;


    /**
     * 商品编码
     */
    @Column(length = 50, nullable = false,unique = true)
    private String productCode;

    /**
     * 商品简介
     */
    @Column
    private String breif;

    /**
     * 搜索标签
     */
    @Column(length = 255)
    private String tags;

    /**
     * 商品品牌Id
     */
    @Column
    private Long brandId;

    /**
     * 商品分类Id
     */
    @Column
    private Long categoryId;

    /**
     * 商品产地
     */
    @Column
    private String productArea;

    /**
     * 商品类型
     * 0=普通商品,1=特有商品,2=券商品,6=黄金商品)
     */
    @Column
    @Enumerated(EnumType.STRING)
    private GoodsKind goodsKind;

    /**
     * 销售规格
     */
    @Column(length = 20)
    private String goodsSpec;

    /**
     * 销售单位
     */
    @Column(length = 20)
    private String unitName;

    /**
     * 保质期单位
     * 0=日 1=月 2=年
     */
    @Column
    private Integer qaDaysUnit;

    /**
     * 保质期
     */
    @Column
    private Integer qaDays;

    /**
     * 最低存储温度
     */
    @Column
    private Double lowTemp;

    /**
     * 最高存储温度
     */
    @Column
    private Double highTemp;



    /**
     * 灵活的商品扩展属性
     * List
     * @see ExtendProductVo
     */
    @Column(columnDefinition = "TEXT")
    private String extendProduct;

    /**
     * 商品介绍(富文本形式)
     */
    @Column(columnDefinition = "TEXT")
    @Convert(converter = ListStringConverter.class)
    private List<String> introImages;

    /**
     * 商品 Logo
     */
    @Column
    private String logo;

    /**
     * 商品轮播图
     */
    @Column(columnDefinition = "TEXT")
    @Convert(converter = ListStringConverter.class)
    private List<String> images;

    /**
     * 商品销售标签(商品的一种组合方式)
     */
    @ManyToMany
    @JoinTable(name = "pro_sale_tag_product",
            joinColumns = {@JoinColumn(name = "product_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "sale_tag_id", referencedColumnName = "id")})
    private List<SaleTag> saleTags;

    /**
     * 关键字
     */
    @ManyToMany
    @JoinTable(name = "pro_key_word_product",
            joinColumns = {@JoinColumn(name = "product_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "key_word_id", referencedColumnName = "id")})
    private List<Keyword> keywords;

    /**
     * 配置商品
     */
    @OneToMany(cascade = CascadeType.ALL,orphanRemoval = true)
    private List<ProductCascade> productCascades;

    /**
     * 关联商品 (规格商品)
     */
    @ManyToOne
    @JoinColumn(name = "relevance_product_id")
    private RelevanceProduct relevanceProduct;

    /**
     * 商品统计数据
     */
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY,mappedBy = "product")
    private ProductStatistic  productStatistic;

    /**
     * @see CommoditySpec
     * 商品箱规中 需要在前端展示瓶箱切换的数量
     */
    @Column
    private Integer packageNumber;

    /**
     * 商家 价格信息
     */
    @JoinColumn(name = "vendor_price_id")
    private VendorPrice vendorPrice;

    /**
     * 商品重量(单位: 克)
     */
    @Column
    private Integer weight;

    /**
     * 订货包装含量
     * 整箱中的数量
     */
    @Column
    private Integer orderPkNumber;

    /**
     * 商品状态
     */
    @Column
    @Enumerated(EnumType.STRING)
    private GbStatus gbStatus;

    /**
     * 建档时间
     */
    @Column
    private Timestamp inDate;

    /**
     * 最低折扣率
     */
    @Column
    private Double minZk;

    /**
     * 是否在审核(修改审核)
     */
    @Column
    private Boolean inAudit;

    /**
     * SEO 关键字信息
     */
    @Column
    private String seoKeywords;


    /**
     * 删除标识
     */
    @Column
    private Boolean deleteFlag = Boolean.FALSE;

    /**
     * 是否锁定商品(如果锁定, 商家不能修改)
     */
    @Column
    private Boolean locked = Boolean.FALSE;

    /**
     * 是否是组合商品
     */
    private Boolean isRapidProduct = Boolean.FALSE;

    /**
     * 组合商品
     */
    @OneToOne(mappedBy = "product", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private GroupProduct groupProduct;

    /**
     * 上下架总开关
     */
    @Column
    @Enumerated(EnumType.STRING)
    private SaleStatus saleStatus;

    /**
     * 上架生效时间
     */
    @Column
    private Timestamp onSaleEffectTime;

    /**
     * 下架生效时间
     */
    @Column
    private Timestamp offSaleEffectTime;

    @Column
    @Convert(converter = SaleChannelEnumSetConverter.class)
    private Set<SaleChannel> saleChannels;

    /**
     * 微商城渠道
     */
    @Column
    @Enumerated(EnumType.STRING)
    private SaleStatus webSaleStatus;

    /**
     * APP渠道
     */
    @Column
    @Enumerated(EnumType.STRING)
    private SaleStatus appSaleStatus;

    /**
     * 积分比例
     */
    @Column
    private Integer pointsProportion;

    /**
     * 是否显示库存
     */
    @Column
    private Boolean showStock=Boolean.FALSE;

    /**
     * 是否显示关联商品
     */
    @Column
    private Boolean showRelevant=Boolean.FALSE;

    /**
     * 全局排序
     */
    @Column
    private Integer globalIdx=9999;

    @ManyToOne
    @JoinColumn(name = "custom_model_id")
    private CommodityCustomModel commodityCustomModel;

    public List<Keyword> getKeywords() {
        return keywords;
    }

    public void setKeywords(List<Keyword> keywords) {
        this.keywords = keywords;
    }


    public String getSaleTagIds() {
        if (CollectionUtils.isEmpty(this.saleTags)) {
            return null;
        }
        List<Long> saleTagIds = Lists.newArrayList();
        for (SaleTag saleTag : this.saleTags) {
            if (Objects.equals(saleTag.getDeleteFlag(),Boolean.TRUE)||Objects.equals(saleTag.getStatus(), IStatus.DELETE)){
                continue;
            }
            saleTagIds.add(saleTag.getId());
        }
        return StringUtils.join(saleTagIds, ",");
    }

    public List<Long> getSaleTagIdList() {
        if (CollectionUtils.isEmpty(this.saleTags)) {
            return Collections.emptyList();
        }
        List<Long> saleTagIds = Lists.newArrayList();
        for (SaleTag saleTag : this.saleTags) {
            if (Objects.equals(saleTag.getDeleteFlag(),Boolean.TRUE)||Objects.equals(saleTag.getStatus(), IStatus.DELETE)){
                continue;
            }
            saleTagIds.add(saleTag.getId());
        }
        return saleTagIds;
    }

    public List<ExtendProductVo> getExtendProducts(){
        if (StringUtils.isNotBlank(this.extendProduct)){
            return JsonUtils.json2Obj(this.extendProduct,List.class,ExtendProductVo.class);
        }
        return Collections.emptyList();
    }
}
